#include "tm1640_driver.h"
#include <stdint.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"

typedef union
{
    uint8_t Buffer[TM1640_DISPLAY_MAX_SIZE];
    struct
    {
        /* 这里可以对每bit位进行操作 */
        uint16_t Speed_L : 16; //速度低
        uint16_t Speed_H : 16; //速度高
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
        uint8_t  :  8;
    }Bits;
}WHT_TM1640_Display_Buffer_t;

/* 真值表 */
static const uint16_t Speed_Table[] =
{
    0x3fe7,// 0
    0x3060,// 1
    0x0ff9,// 2
    0x3cf9,// 3
    0x307e,// 4
    0x3c9f,// 5
    0xff9f,// 6
    0x30e1,// 7
    0x3fff,// 8
    0x3cff,// 9
};


typedef enum
{
    Speed_Type,    //速度
    /* 待添加其它 */
    All_Type,      //所有
}WHT_Display_Type_enum;



static WHT_TM1640_Display_Buffer_t WHT_TM1640_Display_Buffer;

/* 设置显示状态 */
static void WHT_TM1640_Set_Status(WHT_Display_Type_enum type, uint32_t value)
{
    switch (type)
    {
    case Speed_Type:
        WHT_TM1640_Display_Buffer.Bits.Speed_L = value < 100 ? Speed_Table[value%10] : Speed_Table[9];
        WHT_TM1640_Display_Buffer.Bits.Speed_H = value < 100 ? Speed_Table[value/10] : Speed_Table[9];
        break;
    /* 待添加其它 */
    case All_Type:
        memset(WHT_TM1640_Display_Buffer.Buffer, value, sizeof(WHT_TM1640_Display_Buffer.Buffer));
        break;
    }
}




void tm1640_main(void)
{
    uint8_t speed = 0;

    vTaskDelay(100);

    WHT_TM1640_Driver.WHT_Init();
    WHT_TM1640_Set_Status(All_Type, 0);
    WHT_TM1640_Driver.WHT_Display_Buffer(0, WHT_TM1640_Display_Buffer.Buffer, sizeof(WHT_TM1640_Display_Buffer.Buffer));
    WHT_TM1640_Driver.WHT_Set_Brightness(5);
    for (; ;)
    {
        WHT_TM1640_Set_Status(Speed_Type, speed++);
        WHT_TM1640_Driver.WHT_Display_Buffer(0, WHT_TM1640_Display_Buffer.Buffer, sizeof(WHT_TM1640_Display_Buffer.Buffer));
        if (speed >= 100)
        {
            speed = 0;
        }
        vTaskDelay(333);
    }
}

